<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>角色新增/编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">

</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="padding-top: 5px;border: hidden">
    <form class="layui-form" action="" lay-filter="dataForm">
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color: red;">*</span>角色ID</label>
            <div class="layui-input-block">
                <input type="text" id="ipt_roleId" name="roleId" lay-verify="required|strOrNum|min3"  required autocomplete="off" placeholder="角色id"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color: red;">*</span>角色名称</label>
            <div class="layui-input-block">
                <input type="text" name="roleName" lay-verify="required"  required autocomplete="off" placeholder="角色名称"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color: red;">*</span>角色权限</label>
            <div class="layui-input-block" >
                <div id="xtreeRes" style="border:1px solid; border-color:#D2D2D2!important;padding: 10px 0 25px 5px;overflow: auto;height: 300px;"></div>
            </div>
        </div>
        <div class="layui-form-item layui-form-text layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="description" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <div class="layui-input-block" style="text-align: center;">
                <button class="layui-btn" lay-submit="" lay-filter="formSubmit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</fieldset>
<script src="../assets/layui/layui.js"></script>
<script src="../assets/layui-xtree/layui-xtree.js"></script>
<script>
    layui.config({
        base: '../js/'
    });
    layui.use(['jquery','form','validate','common'], function () {
        var form = layui.form,
            $ = layui.jquery,
            validate = layui.validate;
        common = layui.common;
        validate.regVerify();//注册验证
        var id = common.getUrlParam("id");
        var type = common.getUrlParam("type");
        if (!type) type="add";  //默认为add
        var xtree1;
        if (type=="add"){
            //加载tree
            $.ajax({
                url: "./common/resTree",
                dataType: "json",
                success: function (rtn) {
                    if (rtn && rtn.code===0) {
                        xtree1 = new layuiXtree({
                            elem: 'xtreeRes'  //(必填) 放置xtree的容器id，不要带#号
                            , form: form    //(必填) layui 的 from
                            , data: rtn.data
                            , initCheckIds:[]
                        });
                    }
                }
            });

        }
        if (type=="edit"){
            //加载数据
            $("#ipt_roleId").attr("readonly","readonly");

            //获取用户信息
            $.ajax({
                url: "role/info",
                dataType: "json",
                data: { id: id },
                success: function (rtn) {
                    if (rtn && rtn.code===0) {
                        var data = rtn.data;
                        form.val('dataForm',{
                            roleId:data.roleId,
                            roleName:data.roleName,
                            description:data.description,
                        });
                        //xtree1.CheckNodes(data.rights);
                        //加载tree
                        $.ajax({
                            url: "./common/resTree",
                            dataType: "json",
                            success: function (rtn) {
                                if (rtn && rtn.code===0) {
                                    xtree1 = new layuiXtree({
                                        elem: 'xtreeRes'  //(必填) 放置xtree的容器id，不要带#号
                                        , form: form    //(必填) layui 的 from
                                        , data: rtn.data
                                        , initCheckIds:data.rights
                                    });
                                }
                            }
                        });
                    }
                }
            });
        }

        //监听提交
        form.on('submit(formSubmit)', function (data) {
            //common.consoleDebug(JSON.stringify(data.field));
            var checkArr = xtree1.GetCheckedWithParent();
            var ids='';
            for(var i=0;i<checkArr.length;i++){
                ids+=checkArr[i].value+',';
            }
            if(common.endWith(ids,",")){
                ids = ids.substring(0,ids.length-1);
            }
            data.field.rights=ids;

            $.ajax({
                type: 'post',
                url: 'role/'+(type=="edit"?"edit":"add"),
                data: data.field,
                success: function(rtn){
                    if (rtn.code===0) {
                        common.showInfo('处理成功!',function(){
                            //刷新前页
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.document.getElementById('btnSearch').click();
                            parent.layer.close(index);
                        });
                    }
                    else{
                        common.showError(rtn.msg);
                    }
                },
                error: function(XmlHttpRequest, textStatus, errorThrown){
                    common.showError('保存错误');
                }
            });
            return false;
        });
    });
</script>
</body>
</html>